package com.luoq.demo.controller;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@Slf4j
public class HelloController {


    @GetMapping("/hello")
    public String hello() {
        log.info("info -- hello");
        log.warn("warn -- hello");
        log.error("error -- hello");
        log.debug("debug -- hello");
        return "Hello,World!";
    }

    @PostMapping("/changeLog")
    public String changeLog(@NonNull String packageName, @NonNull String logLevel) {
        try {
            LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
            loggerContext.getLogger(packageName).setLevel(Level.valueOf(logLevel));
        } catch (Exception e) {
            log.error("动态修改日志级别出错，原因：" + e.getMessage(), e);
            return "change log fail";
        }
        return "change log success";
    }
}
